package cn.iocoder.yudao.module.engine.controller.app.usertimebalance;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.engine.controller.app.usertimebalance.vo.AppUserTimeBalanceRespVO;
import cn.iocoder.yudao.module.resource.api.IUserTimeBalanceApi;
import cn.iocoder.yudao.module.resource.api.IUserTimeTransactionApi;
import cn.iocoder.yudao.module.resource.api.enums.UserTimeTransactionTypeEnum;
import cn.iocoder.yudao.module.resource.api.model.UserTimeBalanceDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户时长余额")
@RestController
@RequestMapping("/lm/user-time-balance")
@Validated
public class AppUserTimeBalanceController {
    @Resource
    private IUserTimeBalanceApi userTimeBalanceApi;
    @Resource
    private IUserTimeTransactionApi userTimeTransactionApi;

    @GetMapping("/get")
    @Operation(summary = "获得用户时长余额")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthenticated
    public CommonResult<AppUserTimeBalanceRespVO> getUserTimeBalance() {
        final Long userId = getLoginUserId();

        final UserTimeBalanceDTO dto = userTimeBalanceApi.getUserTimeBalanceByUserIdWithNewUserGive(userId);
        final AppUserTimeBalanceRespVO vo = BeanUtils.toBean(dto, AppUserTimeBalanceRespVO.class);

        final Boolean paid = userTimeTransactionApi.hasUserTimeTransactionByType(userId, UserTimeTransactionTypeEnum.PURCHASE);
        vo.setPaid(paid);
        return success(vo);
    }

}